package 二叉树题目.判断是不是平衡二叉树;

public class Solution {
    public int maxDepth(TreeNode root) {
        // write code here
        if (root == null) {
            return 0;
        }
        int leftH = maxDepth(root.left);
        int leftR = maxDepth(root.right);
        return leftH > leftR ? leftH + 1 : leftR + 1;
    }

    public boolean IsBalanced_Solution(TreeNode root) {
        if (root == null) {
            return true;
        }
        int leftH = maxDepth(root.left);
        int rightH = maxDepth(root.right);
        if (Math.abs(leftH - rightH) > 1) {
            return false;
        }
        return IsBalanced_Solution(root.right)
                && IsBalanced_Solution(root.left);
    }
}
